package main.model.hexisland;

import main.model.hexisland.location.HexLocation;

import java.util.ArrayList;
import java.util.Collection;

/**
 * All hexagon formulas were taken or inspired by http://www.redblobgames.com/grids/hexagons/
 * The greatest degree of kudos to Amit Patel of http://www.redblobgames.com/
 *
 * User: gabe
 * Date: 11/06/13
 * Time: 19:00
 */
public class TileUtil {

    /**
     * The 'as-the-crow-flies' distance between two tile locations.
     * @param one
     * @param two
     * @return
     */
    public static int findDistance(HexLocation one, HexLocation two){
        return (Math.abs(one.getX() - two.getX()) +
                Math.abs(one.getY() - two.getY()) +
                Math.abs(one.getZ() - two.getZ()) ) / 2;
    }

    /**
     * Gets the co-ordinates of the adjacent neighbour in the given direction
     * @param current
     * @param direction
     * @return
     */
    public static HexLocation findNeighbour(HexLocation current, AdjacentBearing direction){
        return findNeighbour(current,direction.getBearingValue());
    }

    /**
     * Gets the co-ordinates of the adjacent neighbour in the given direction.
     * @param current
     * @param direction
     * @return
     */
    public static HexLocation findNeighbour(HexLocation current, int direction){
        int parity =  current.getR() & 1;
        return new HexLocation(
                current.getQ() + adjacentNeighbors[parity][direction][0],
                current.getR() + adjacentNeighbors[parity][direction][1]);
    }

    public static Collection<HexLocation> findAllNeighbours(HexLocation current){
        Collection<HexLocation> neighbours = new ArrayList<HexLocation>();
        for(int i = 0; i<6;i++){
            neighbours.add(findNeighbour(current,i));
        }
        return neighbours;
    }

    /**
     * The transforms for getting each co-ordinate.
     * The first array divides the entries by parity,
     * the second array divides by direction and,
     * the final array are the transformations for Q and R.
     */
    private static final int [][][] adjacentNeighbors = {
            {   {+1,  0}, { 0, -1}, {-1, -1},
                {-1,  0}, {-1, +1}, { 0, +1}    },
            {   {+1,  0}, {+1, -1}, { 0, -1},
                {-1,  0}, { 0, +1}, {+1, +1}   }   };

    /**
     * The tile in the diagonal direction from the current location.
     * @param current
     * @param direction
     * @return
     */
    /*public HexLocation findDiagonal(HexLocation current, DiagonalBearing direction){
        return new HexLocation(
                current.getX() + diagonals[direction.getBearing()][0],
                current.getY() + diagonals[direction.getBearing()][1],
                current.getZ() + diagonals[direction.getBearing()][2]);
    }

    /**
     * The transformation for finding the diagonal from a position
     */
    /*private static final int [][] diagonals =  {};



    /**
     * An Enum to translate the numerical representations of directions for diagonal tiles into words
     */
    /*public enum DiagonalBearing{
        NORTH_EAST  (0), NORTH       (1),
        NORTH_WEST  (2), SOUTH_WEST  (3),
        SOUTH       (4), SOUTH_EAST  (5);

        private int bearing;

        private DiagonalBearing(int bearing){ this.bearing = bearing;}

        public int getBearing(){ return bearing;}
    } */
}
